precision mediump float;
uniform sampler2D blend;
uniform sampler2D grass;
uniform sampler2D dirt;
uniform sampler2D rock;
uniform sampler2D water;
uniform sampler2D road;
uniform vec3 u_CameraPosition;

varying vec2 v_VertTexCoord;
varying vec3 v_VertPositionL;
varying vec3 v_VertPosition;
varying vec3 v_VertNorm;
void main()
{
	vec4 Blend = texture2D(blend, v_VertTexCoord/20.0);
	vec4 Grass = texture2D(grass, v_VertTexCoord);
	vec4 Dirt = texture2D(dirt, v_VertTexCoord);
	vec4 Rock = texture2D(rock, v_VertTexCoord);
	vec4 Water = texture2D(water, v_VertTexCoord);
	vec4 Road = texture2D(road, v_VertTexCoord);
	
	vec4 FinalObjColor;
	//Render map texture follow heightfilter value
	//	-lv1 (0-5): water;
	//	-lv2 (5-10): water + sand;
	//	-lv3 (10-50): sand;
	//	-lv4 (50-70): sand + grass;
	//	-lv5 (70-150): grass;
	//	-lv6 (150-200): grass + rock;
	//	-lv7 (200-255): rock;
	float scaleRage = 2.0;
	float height = v_VertPositionL.y*0.5;
	if(height < 30.0)
	{
		FinalObjColor = Water;
	}
	else if(height >= 30.0 && height < 35.0)
	{
		FinalObjColor = ((35.0 - height)*Water + (height - 30.0)*Dirt)/5.0;
	}
	else if(height < 50.0)
	{
		FinalObjColor = Dirt;
	}
	else if(height >= 50.0 && height < 60.0)
	{
		FinalObjColor = ((60.0 - height)*Dirt + (height - 50.0)*Grass)/10.0;
	}
	else if(height < 85.0)
	{
		FinalObjColor = Grass;
	}
	else if(height >= 85.0 && height < 100.0)
	{
		FinalObjColor = ((100.0 - height)*Grass + (height - 85.0)*Rock)/15.0;
	}
	else
	{
		FinalObjColor = Rock;
	}
	
	FinalObjColor = (Road * Blend.r + FinalObjColor * Blend.g + FinalObjColor * Blend.b) / (Blend.r + Blend.g + Blend.b);
	
	
	vec4 cAmbientComponent = vec4(1.0, 1.0, 1.0, 1.0);
	vec3 vToEye = normalize(u_CameraPosition - v_VertPosition);
	vec3 vVertNorm = vec3(normalize(v_VertNorm));
	
	//First Lighting Source
	vec3 vLightDirection = normalize(v_VertPosition - vec3(50.0, 300.0,10.0));
	vec4 cDiffuseComponent = max(dot(vVertNorm, -vLightDirection), 0.0) * vec4(1.0, 1.0, 1.0, 1.0); //White light
	vec3 vReflect = normalize(reflect(vLightDirection, vVertNorm));
	vec4 cSpecularComponent = pow(max(dot(vReflect, vToEye), 0.0), 20.0) * vec4(1.0, 1.0, 1.0, 1.0); //While light
	
	float fWeight = 0.4;
	vec4 cFinal = FinalObjColor;//vec4(((cAmbientComponent * fWeight + (cDiffuseComponent) * (1.0 - fWeight)) * FinalObjColor).xyz, FinalObjColor.w);
	
	float DistanceToCamera = length(u_CameraPosition - v_VertPosition);
	float LerpValue = clamp((DistanceToCamera - 200.0)/1000.0, 0.0, 1.0);
	
	gl_FragColor = cFinal;//LerpValue * vec4(1.0, 1.0, 1.0, 1.0) + (1.0-LerpValue)*cFinal;
}
